#include <windows.h>


HWND myCreateBox(HWND hwnd, int id, char* type, char* s, 
                 int x, int y, int width, int height)
// Creates a box control: frame, static box, edit box or list box
// hwnd is the window within which the box appears,
// id is a user-provided ID number,
// type= 'frame' or 'static' or 'edit' or 'list' (only the 1st character is used)
//       for edit boxes, include a 1 (e.g. 'edit1') to specify a single-line box
// s = string to be displayed (ignored for a frame)
// x,y = position on screen,
// width, height = width and height of the box.
{ HWND h;

  char typechar = type[0];
  DWORD styleex;
  DWORD style;
  char* classname;
  switch ( typechar )
  { 
    case 'f': case 'F':
       styleex = WS_EX_WINDOWEDGE;
       style = WS_CHILD | WS_VISIBLE | SS_BLACKFRAME;
       classname = "STATIC";
    break;
    case 's': case 'S':
       styleex = WS_EX_STATICEDGE;
       style = WS_CHILD | WS_VISIBLE | SS_LEFT | SS_NOPREFIX;
       classname = "STATIC";
    break;
    case 'e': case 'E':  
    {  styleex = WS_EX_CLIENTEDGE;
       style = WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL;
       // now search for a '1' in type. If none found, then specify a multi-line
       // edit box. We don't use strchr() so we don't need a #include.
       char* c = type;
       while( (*c != '1') && (*c != 0) ) ++c;
       if (*c==0) style |= ES_MULTILINE | ES_AUTOVSCROLL;
       // for scroll bars specify | WS_VSCROLL | WS_HSCROLL
       classname = "EDIT";
    }
    break;
    case 'l': case 'L': 
       styleex = WS_EX_CLIENTEDGE;
       style = WS_CHILD | WS_VISIBLE | WS_VSCROLL | LBS_NOTIFY;
       classname = "LISTBOX";
    break;
    default:
          MessageBox(hwnd, "unrecognised box type in myCreateBox.", "Error", MB_OK | MB_ICONERROR);
  }     
  
  h = CreateWindowEx(styleex, classname, "", style, x,y,width,height,
            hwnd, (HMENU) id, GetModuleHandle(NULL), NULL);

  if (h == NULL)
  {   MessageBox(hwnd, "myCreateBox could not create box.", "Error", MB_OK | MB_ICONERROR);
      return h;
  }

  // else
  // set the font
  HFONT hfont;
  hfont = (HFONT) GetStockObject(DEFAULT_GUI_FONT);
  SendMessage(h, WM_SETFONT, (WPARAM)hfont, MAKELPARAM(FALSE, 0));

  // set the text
  switch ( typechar )
  { 
    case 'f': case 'F': break;
    case 's': case 'S': case 'e': case 'E':
       SendMessage(h, WM_SETTEXT, 0, (LPARAM) s); break;
    case 'l': case 'L': 
       SendMessage(h, LB_ADDSTRING, 0, (LPARAM) s); break;
  }
  
  return h;
}